﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 7. Write a Function that returns the index of the first 
			element in array that is bigger than its neighbors, or 
			-1, if there’s no such element.
			* Use the function from the previous exercise.  -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function isBiggerThanNeighbors(position, array) {
			var result = 0;

			if (position <= 0 || position >= (array.length - 1)) {
				result = -1;
			}
			else {
				if (array[position] > array[position + 1] &&
					array[position] > array[position - 1]) {
					result = 1;
				}
			}

			return result;
		}

		// Process user input
		var numbers = new Array();
		var input = prompt("Enter numbers separated by spaces");
		var array = input.split(' ');

		// Collect numbers only
		var j = 0;
		for (var i in array) {
			var num = parseInt(array[i]);
			if (!isNaN(num)) {
				numbers[j] = num;
				j++;
			}
		}
		// If there are numbers entered
		if (numbers.length > 0) {
			jsConsole.write("All numbers: ");
			jsConsole.writeLine(numbers.join());

			// Search for bigger item
			var result = -1;
			for (var i = 1; i < (numbers.length - 1); i++) {
				if (isBiggerThanNeighbors(i, numbers) == 1) {
					result = i;
					break;
				}
			}

			// Write the result
			jsConsole.writeLine("The index of the first element in array");
			jsConsole.writeLine("that is bigger than its neighbors is: " + result);
		}
	</script>
</body>
</html>
